import { _decorator, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { Rank, RankOper } from '../entity/Rank';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { RankDetail } from './RankDetail';
import { Utils } from '../common/Utils';
import { UserInfo } from '../common/UserInfo';
const { ccclass, property } = _decorator;

@ccclass('AttackItem')
export class AttackItem extends Component {
    @property(Label)
    index:Label
    @property(Label)
    playerName:Label
    @property(Label)
    people:Label
    @property(Label)
    village:Label
    @property(Label)
    point:Label
    @property(SpriteFrame)
    activeBg:SpriteFrame

    private rank:Rank
    start() {
        this.playerName.node.on(Node.EventType.TOUCH_END, this.onTouch, this)
    }

    setData(rank:Rank,oper: RankOper) {
        this.rank = rank
        
        this.playerName.string = rank.PlayerName
        this.people.string = rank.PlayerPeople + ''
        this.village.string = rank.VillageNum + ''

        Utils.handleEllipsis(this.playerName.node)

        if(oper == RankOper.Attack){
            this.index.string = rank.PlayerAttackRank + ''
            this.point.string = rank.PlayerAttackPoint + ''
        } else {
            this.index.string = rank.PlayerDefenseRank + ''
            this.point.string = rank.PlayerDefensePoint + ''
        }
        
        if(this.rank.PlayerId == UserInfo.instance.villageData.uid){
            this.getComponent(Sprite).spriteFrame = this.activeBg
        }
    }

    async onTouch(){
        if(!this.rank){
            return
        }
        let ui = await UIManager.instance.openUI(PrefabEnum.RankDetail)
        ui.getComponent(RankDetail).setData(this.rank.PlayerId)
    }
}


